local P = {}
GUIFun = P

local GUILayout = UnityEngine.GUILayout
local GUI = UnityEngine.GUI
local Screen = UnityEngine.Screen

local controlHeight = 50
local controlMinWidth = 200

function P.Toolbar(cur, arr, label, space, defaultValue)
	if not cur then
		cur = defaultValue
	end

	GUILayout.BeginHorizontal()

	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label)
	end

	local pos, v = table.find(arr, function(k, v) return v == cur end)
	if not pos then
		pos = 0
	else
		pos = pos - 1
	end
	local array = tolua.toarray(arr, typeof(System.String))
	pos = GUILayout.Toolbar(pos, array, P.Height(controlHeight))
	pos = pos + 1
	local ret = arr[pos]

	GUILayout.FlexibleSpace();
	GUILayout.EndHorizontal()
	return ret
end

function P.NumberField(num, label, horizontal, space, defaultValue, min, max)
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label)
	end

	if num == nil then
		if defaultValue then
			num = defaultValue
		else
			num = 0
		end
	end

	-- if min and max then
	-- 	num = GUILayout.HorizontalSlider(tonumber(num), min, max, P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth))
	-- end

	-- local ret = tonumber(GUILayout.TextField(tostring(num), P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth / 2)))

	-- if min and max then
	-- 	num = tostring(GUILayout.HorizontalSlider(tonumber(num), min, max, P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth)))
	-- end

	local ret = GUILayout.TextField(tostring(num), P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth / 2))

	if horizontal then
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal()
	end
	if not ret then
		ret = 0
	end
	return ret
end

function P.IdField(num, label, horizontal, space, defaultValue, min, max)
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label)
	end

	if num == nil then
		if defaultValue then
			num = defaultValue
		else
			num = 0
		end
	end

	if min and max then
		num = GUILayout.HorizontalSlider(tonumber(num), min, max, P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth))
	end

	local ret = tonumber(GUILayout.TextField(tostring(num), P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth / 2)))

	if horizontal then
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal()
	end
	if not ret then
		ret = 0
	end
	return ret
end

function P.TextField(text, label, horizontal, space, defalutValue)
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label)
	end

	if text == nil then
		if defaultValue then
			num = defaultValue
		else
			text = ""
		end
	end

	local ret = GUILayout.TextField(text, P.Height(controlHeight), GUILayout.MinWidth(controlMinWidth))
	if horizontal then
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal()
	end
	return ret
end


function P.TextArea(text, label, horizontal, space, defalutValue)
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label)
	end

	if text == nil then
		if defaultValue then
			num = defaultValue
		else
			text = ""
		end
	end

	local ret = GUILayout.TextArea(text, GUILayout.MinWidth(controlMinWidth))
	if horizontal then
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal()
	end
	return ret
end

function P.Toggle(toggle, label, space, defaultValue)
	if toggle == nil then
		toggle = defaultValue
	end

	GUILayout.BeginHorizontal()
	if space then
		GUILayout.Space(space)
	end
	if label then
		GUILayout.Label(label)
	end
	-- local ret = GUILayout.Toggle(toggle, "", P.Height(controlHeight), P.Width(controlHeight))
	if GUILayout.Button(toggle and "true" or "false", P.Height(controlHeight)) then
		toggle = not toggle
	end
	GUILayout.FlexibleSpace()
	GUILayout.EndHorizontal()
	return toggle
end

function P.Button(label, horizontal, space)
	if label == nil then
		label = ""
	end
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end

	local ret = GUILayout.Button(label, P.Height(controlHeight))

	if horizontal then
		GUILayout.FlexibleSpace()
		GUILayout.EndHorizontal()
	end
	return ret
end

function P.Vector3Field(vec3, label, horizontal, space)
	if not vec3 then
		vec3 = Vector3.zero
	end

	if horizontal then
		GUILayout.BeginHorizontal()
	end
	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(label.."")
	end
	vec3.x = P.NumberField(vec3.x, "  x")
	vec3.y = P.NumberField(vec3.y, "  y")
	vec3.z = P.NumberField(vec3.z, "  z")
	if horizontal then
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal()
	end
	return vec3
end

function P.ToNumberVector3(v)
	v.x = tonumber(v.x)
	v.y = tonumber(v.y)
	v.z = tonumber(v.z)
	return v
end

function P.ColorField(color, label, space)
	if not color then
		color = Color.white
	end

	GUILayout.BeginHorizontal()
	if space then
		GUILayout.Space(space)
	end

	if label then
		GUILayout.Label(tostring(label))
	end
	color.r = P.NumberField(color.r, "  r")
	color.g = P.NumberField(color.g, "  g")
	color.b = P.NumberField(color.b, "  b")
	color.a = P.NumberField(color.a, "  a")
	GUILayout.FlexibleSpace();
	GUILayout.EndHorizontal()
	return color
end

function P.Label(label, horizontal, space)
	if horizontal then
		GUILayout.BeginHorizontal()
	end
	
	if space then
		GUILayout.Space(space)
	end
	GUILayout.Label(tostring(label))

	if horizontal then
		GUILayout.FlexibleSpace()
		GUILayout.EndHorizontal()
	end
end

function P.LabelAndDelButton(label, horizontal, space)
	if horizontal then
		GUILayout.BeginHorizontal()
	end

	if space then
		GUILayout.Space(space)
	end
	GUILayout.Label(label)
	local ret = GUILayout.Button("delete", P.Height(controlHeight))

	if horizontal then
		GUILayout.FlexibleSpace()
		GUILayout.EndHorizontal()
	end
	return ret
end

function P.Height(height)
	return GUILayout.Height(P.ConvertSize(height))
end

function P.Width(width)
	return GUILayout.Width(P.ConvertSize(width))
end

function P.ConvertSize(size)
	if UnityEngine.Application.isEditor then
		return size * (UnityEngine.Screen.dpi / 108.5) * (UnityEngine.Screen.width / 3500);
	end
	
	return size * UnityEngine.Screen.width / 1920;
end


function P.Init()
	GUI.skin.label.fontSize = GUIFun.ConvertSize(40)
	GUI.skin.textField.fontSize = GUIFun.ConvertSize(40)
	GUI.skin.textArea.fontSize = GUIFun.ConvertSize(40)
	GUI.skin.button.fontSize = GUIFun.ConvertSize(40)
	GUI.skin.toggle.fontSize = GUIFun.ConvertSize(40)
	GUI.skin.horizontalSlider.fixedHeight = GUIFun.ConvertSize(40)
	GUI.skin.horizontalSliderThumb.fixedWidth = GUIFun.ConvertSize(40)
	GUI.skin.horizontalSliderThumb.fixedHeight = GUIFun.ConvertSize(40)
	GUI.skin.verticalScrollbar.fixedWidth = GUIFun.ConvertSize(100)
	GUI.skin.verticalScrollbarThumb.fixedWidth = GUIFun.ConvertSize(100)
	GUI.skin.horizontalScrollbar.fixedHeight = GUIFun.ConvertSize(100)
	GUI.skin.horizontalScrollbarThumb.fixedHeight = GUIFun.ConvertSize(100)
end